//! The stock handler class
/*!
  Loads all stocks stored in in table "ALL_STOCKS".
  Can add a new stock into this table.
  Can save stock values into the stock table.
*/

#ifndef STOCK_H
#define STOCK_H

#include <QObject>
#include <QMap>
#include <QDateTime>
#include <QStringList>
#include <QList>
#include <QMap>
#include <QStandardItemModel>
#include <QDebug>
#include "instruments/instruments.h"
#include "config/config.h"
//#include "global/global.h"

#define STOCK_TABLE "ALL_STOCKS"
#define STOCK_NAME_FIELD    "STOCK_NAME"
#define STOCK_SYMBOL_FIELD  "STOCK_SYMBOL"

#define VALUE_SYMBOLS  "l10,t10,c10,p20,g00,h00,v00,j10,a00,b00,a50,b60"
#define VALUE_NAMES  "Kurs,Zeit,Veraenderung,Veraenderung(%),Tagestief,Tageshoch,Volumen,Marktkapitalisierung,Brief,Geld,Briefvolumen,Geldvolumen"

//#define SET_CURRENT_TIME                    //sets the current time, if no time was sent with the stream

//! normal usage
/*!
  use the stock class for normal usage
*/
#define NO_FLAG 0x00

//! Does not load any stocks from db
/*!
  Use it, if you want to use the stock class in a depot class
*/
#define LOAD_NO_STOCKS  0x01

//! flag for the addStock function
/*!
  Use it, if you just want to save the stock name in the list
*/
#define ADD_STOCK_NAME  0x02

class Stock : public Instruments
{
   // Q_OBJECT
public:
    //! A constructor
    /*!
      \param flag Sets the flag, how the class will be used
      \param parent The parent object
    */
    explicit Stock(int flag=NO_FLAG,QObject *parent = 0);

    explicit Stock(const Stock& stock);

    //! An overloaded constructor
    /*!
      Creates an own database with the given filename dbName
      \param dbName Set the db-file, in wgich all stock data will be stored
      \param flag Sets the flag, how the class will be used
      \param parent The parent object
    */
    explicit Stock(const QString& dbName,int flag=NO_FLAG,QObject *parent=0);

    //! An overloaded constructor
    /*!
      \param cfg A pointer to a before initiated config class, will be not deleted
      \param flag Sets the flag, how the class will be used
      \param parent The parent object
    */
    explicit Stock(Config *cfg,int flag=NO_FLAG,QObject *parent=0);

    //! An overloaded constructor
    /*!
      \param cfg A pointer to a before initiated config class, will be not deleted
      \param dbName Set the db-file, in wgich all stock data will be stored
      \param flag Sets the flag, how the class will be used
      \param parent The parent object
    */
    explicit Stock(Config* cfg,const QString& dbName,int flag=NO_FLAG,QObject *parent=0);

    ~Stock();
    //! Adds a stock to the internal map
    /*!
      Adds the given stock symbol to the internal map, if it is still not in the map.
      If the flag ADD_STOCK_NAME is set, the function handles the parameter stock as a stock name.
      \param stock Have to be a stock symbol or a stock name if ADD_STOCK_NAME is set
      \param flag Use ADD_STOCK_NAME to add a stock name or use the standard value NO_FLAG if you want to add a stock symbol
      \return An iterator of the map at the insert position
    */
    QMap<QString,QList<QStandardItem*> >::iterator addStock(const QString& Stock, int flag=NO_FLAG,const QMap<QString,QList<QStandardItem*> >& initVals=QMap<QString,QList<QStandardItem*> >());

    //! Sets the value to a specific stock
    /*!
      Set a value to the given stock.
      \param stock The stock symbol
      \param time The timestamp, when the value occured
      \param valname The value symbol of the given value
      \param value The value itself
    */
    void setValue(const QString& Stock,const QString& time, const QString& valname,const QString& value);

    //! Sets more than one value to a given stock
    /*!
      Save values of the value map in the stock db. The key in map is the value symbol and the value part of the map is the value itself.
      \param stockSymbol The stock symbol where the values will be inserted.
      \param vals The value map
    */
    void setValue(const QString& stockSymbol, QMap<QString,qreal>& vals);

    //! Get the QStandardItem of the given stock symbol and the given value symbol
    /*!
      \param stockSymbol The given stock symbol
      \param valSymbol The value symbol of the value that have to be returned
      \return A Pointer to the QStandardItem of the value symbol of the stock with the value data in the text section of the QStandardItem
    */
    QStandardItem* getValue(const QString &stockSymbol, const QString &valSymbol);

    //! Get the QStandardItem with the given row and col (for a QTableView)
    /*!
      \param row The row of the wanted value
      \param col The col of the wanted value
      \return returns the QStandardItem at the given position
    */
    QStandardItem* getItem(int row,int col);

    QStandardItem* getItem(const QString& stockSymbol,int col);

    //! Get all stock names in a list
    /*!
      \return Returns a QStringList with all stock names saved in this class
    */
    QStringList getAllStocks()const;

    //! Get the stock map
    /*!
      \return Returns a QMap with all current saved stocks. Just the last values are saved in this map. Older values are saved in db.
    */
    QMap<QString,QList<QStandardItem*> > getStocks()const;

    //! Overwrite the stock map
    /*!
      \param s The map which overwrites the current map
    */
    void setStocks(const QMap<QString,QList<QStandardItem*> > &s);

    QList<QStandardItem*> getStockValues(const QString& stockSymbol)const;

    QString getStockSymbolByName(const QString& stockName);

    Stock& operator=(const Stock&);


    void setStaticStockNames(const QStringList& stockNames);
    QStringList getStaticStockNames()const;
    void setStockNames(const QStringList& names);
    QStringList getStockNames()const;
    void setStockSymbols(const QStringList& symbols);
    QStringList getStockSymbols()const;
    void setValSymbols(const QStringList& valsymbols);
    QStringList getValSymbols()const;

    void addStock(const QString& stockName,const QString& stockSymbol);
    void addStock(const QStringList& stockNames,const QStringList& stockSymbols);

    void getColumnPairsFromDB(QList<QPointF>*,const QString&,const QString&,const QString&,bool);    //saves pairs of values from two columns in a list


    void init();
    QString convertStockName(const QString& stockName);

private:
    QMap<QString,QList<QStandardItem*> > stocks; //current stock values
    /*static stock names saves all available stock names*/
    QStringList staticStockNames,stockNames,stockSymbols,valSymbols;
    int flag;                                   //flags for this stock-class
};

#endif // STOCK_H
